<!DOCTYPE HTML>
<!--

Copyright 2010 Darryl Parker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

file: wpl-amazon-extension\background.html

-->
<html>
  <head>
  </head>
  <body>
    <script>

      function checkAvailability(isbn, callback) {
      
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function(data) {
          if (xhr.readyState == 4) {
            if (xhr.status == 200) {
              var alert_level = localStorage["alert_level"];
              var data = xhr.responseText;
              var bookAvailable = /Checked In/;
              var notFound = /Sorry, could not find anything matching/;
              var message;
              var found = false;
              
              if (! alert_level) {

                alert_level = "all";
                localStorage["alert_level"] = alert_level;

              }
              
              if (notFound.test(data)) {
              
                if (alert_level != "all"){
                  return;
                }
                
                message = chrome.i18n.getMessage("bookNotFound");
              }
              else {

                found = true;

                if (bookAvailable.test(data)) {
                
                  message = chrome.i18n.getMessage("bookAvailable");
                  
                }
                else {
                
                  if (alert_level == "available")
                  {
                    return;
                  }
                
                  message = chrome.i18n.getMessage("bookNotAvailable");
                }
                
              }
              
              callback({"found":found,"message":message});
              
            }
            else {
              callback(null);
            }
          }
        }

        var url = 'http://ottawa.bibliocommons.com/search?custom_query=Identifier%3A' + isbn;
        xhr.open('GET', url, true);
        xhr.send();
        
      } // checkAvailability
      
      function checkSearch(index, isbn, callback){

        var xhr = new XMLHttpRequest();
        
        xhr.index = index;
        
        xhr.onreadystatechange = function(data) {
          if (xhr.readyState == 4) {
            if (xhr.status == 200) {

              var data = xhr.responseText;
              
              var notFound = /Sorry, could not find anything matching/g;
              
              var found = ! notFound.test(data);
              
              if (! found) {
                callback(this.index, found);
              }

            }
          }
        }

        var url = 'http://ottawa.bibliocommons.com/search?custom_query=Identifier%3A' + isbn;
        xhr.open('GET', url, true);
        xhr.send();

      }  // checkSearch
      
      // wire up the action, passing through the callback
      function onRequest(request, sender, callback) {
        if (request.action == 'checkAvailability') {
          checkAvailability(request.isbn, callback);
        }
        if (request.action == 'checkSearch') {
        
          var check_search = localStorage["check_search"];

          if (! check_search) {

            check_search = "yes";
            localStorage["check_search"] = check_search;

          }
          if (check_search == "yes") {
            checkSearch(request.index, request.isbn, callback);
          }
        }
      };

      chrome.extension.onRequest.addListener(onRequest);
      
    </script>
  </body>
</html>
